package com.kuang.servlet;

import com.kuang.pojo.User;

import com.kuang.service.user.UserService;
import com.kuang.service.user.UserServiceImpl;
import com.kuang.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String userCode = req.getParameter("userCode");
       String userPassword=req.getParameter("userPassword");

        UserService userLoginService = new UserServiceImpl();
        User user=userLoginService.login(userCode,userPassword);
        if (user !=null){
            resp.sendRedirect(req.getContextPath()+"/jsp/frame.jsp");
            req.getSession().setAttribute(Constants.USER_SESSION,user);
        }else{
            req.setAttribute("error","账户名或密码错误");
//            resp.sendRedirect(req.getContextPath()+"/login.jsp");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
